﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using VDataAdapter.BlockingQueue;

namespace VDataAdapter
{
    //class VdataManager
    //{
    //    protected TransactionBuffer _transactionBuffer;
    //    ManualResetEvent _waitFirstTime = new ManualResetEvent(false);
    //    ManualResetEvent _endRecovery = new ManualResetEvent(false);
    //    VdataCommunicator _sender;
    //    WorkerManager _workerManager;
    //    DataSource _dataSource;
    //    CommunicationDataQueue _queue;


    //    public VdataManager() {            
    //        _waitFirstTime = new ManualResetEvent(false);
    //        _endRecovery = new ManualResetEvent(false);
    //        _transactionBuffer = new TransactionBuffer();
    //    }

    //    public void Start()
    //    {
    //        ManualResetEvent waitFirstTime = new ManualResetEvent(false);
    //        ManualResetEvent waitTransaction = new ManualResetEvent(false);

    //        WorkerManager manager = new WorkerManager();
    //        VdataCommunicator sender = new VdataCommunicator(_dataSource, _queue);

    //        //VdataGetFirstTimeWorker firstWorker = new VdataGetFirstTimeWorker(manager, sender, waitFirstTime);

    //        // this worker can only start after first worker
    //        //VdataTransactionsWorker transWorker = new VdataTransactionsWorker(manager, sender, waitFirstTime, waitTransaction);

    //        // this worker can only start after transworker
    //        WorkerWaiter waiter = new WorkerWaiter(waitTransaction, null, 0);

    //        //firstWorker.StartWork(null);
    //        //transWorker.StartWork(null); 


    //    }

    //    public void ReceiveGetFirstTime(IDictionary<string, PriceLibrary.PriceService.StockBase> basePrices,
    //        IDictionary<string, PriceLibrary.PriceService.StockBase> top3Prices)
    //    {
    //        if (basePrices == null && top3Prices == null)
    //            throw new ArgumentNullException("Received null basePrices or top3Prices");

    //        // create worker
    //        FirstTimeWorker worker = new FirstTimeWorker(_waitFirstTime, _sender, _dataSource);
    //        worker.AcceptManager(_workerManager);
            
    //        // create a list of arguments
    //        var firstTimeParams = new Dictionary<string, IDictionary<string, PriceLibrary.PriceService.StockBase>>();
    //        firstTimeParams.Add("BasePrices", basePrices);
    //        firstTimeParams.Add("Top3Prices", top3Prices);
            
    //        worker.StartWork(firstTimeParams);
    //    }

    //    private List<PriceLibrary.ChartService.Transaction> AddToList(List<PriceLibrary.ChartService.Transaction> listToAdd, 
    //        List<PriceLibrary.ChartService.Transaction> newList)
    //    {
    //        foreach (PriceLibrary.ChartService.Transaction trans in newList)
    //        {
    //            listToAdd.Add(trans);
    //        }
    //        return listToAdd;
    //    }

    //    public void ReceiveTransactions(IList<PriceLibrary.ChartService.Transaction> transactionsList, int currentCount, int totalCount)
    //    {
    //        if(_transactionBuffer == null)
    //            throw new NullReferenceException("Transaction Buffer is NULL");

    //        bool receivedAll = _transactionBuffer.ReceivedTransaction(transactionsList, currentCount, totalCount);

    //        if (receivedAll)
    //        {
    //            TransactionWorker worker = new TransactionWorker(_waitFirstTime, _endRecovery, _sender);
    //            worker.AcceptManager(_workerManager);
    //            worker.StartWork(_transactionBuffer.Transactions);
    //        }
    //    }

    //    public void ReceiveMarketInfo(IDictionary<int, PriceLibrary.PriceService.StockBase> marketInfos)
    //    {
    //        MarketInfoWorker worker = new MarketInfoWorker(_waitFirstTime, _sender);
    //        worker.StartWork(marketInfos);
    //    }

    //}
}
